package org.nlp2rdf.navigator.benchmark;

import org.apache.commons.collections15.IteratorUtils;
import org.apache.commons.collections15.Transformer;

import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Set;

/**
 * Created by Claus Stadler
 * Date: Oct 15, 2010
 * Time: 4:59:35 PM
 */
public class TransformedSetView<T, U>
    extends AbstractSet<T>
{
    private Set<U> underlying;
    private Transformer<T, U> toUnderlying;
    private Transformer<U, T> toView;


    public TransformedSetView(Set<U> underlying, Transformer<T, U> toUnderlying, Transformer<U, T> toView)
    {
        this.underlying = underlying;
        this.toView = toView;
        this.toUnderlying = toUnderlying;
    }

    public boolean contains(Object item)
    {
        return underlying.contains(toUnderlying.transform((T)item));
    }

    @Override
    public Iterator<T> iterator() {
        return IteratorUtils.transformedIterator(underlying.iterator(), toView);
    }

    @Override
    public int size() {
        return underlying.size();
    }
}
